package com.pe1n.yangtaopai.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;
import android.widget.Toast;

import com.pe1n.yangtaopai.bean.NetWorkBean;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by My on 2016/10/3.
 */
public class NetWorkReceive extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        //监听网络服务
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();//获取网络信息
        if (info != null) {
            if (info.isConnected()) {
                //获取当前网络类型
                //String type = info.getTypeName();
                //Log.e("Tag", "网络已连接!" + type);
                Toast.makeText(context, "网络已连接,请您等待数据刷新!", Toast.LENGTH_SHORT).show();
                NetWorkBean netWorkBean=new NetWorkBean();
                netWorkBean.setCollectNet(true);
                EventBus.getDefault().postSticky( netWorkBean);
            } else {
                Log.e("Tag", "网络连接异常!");
                Toast.makeText(context, "网络连接异常", Toast.LENGTH_SHORT).show();
                NetWorkBean netWorkBean=new NetWorkBean();
                netWorkBean.setCollectNet(false);
                EventBus.getDefault().postSticky( netWorkBean);
            }
        } else {
            Log.e("Tag", "没有网络!");
            Toast.makeText(context, "没有网络", Toast.LENGTH_SHORT).show();
            NetWorkBean netWorkBean=new NetWorkBean();
            netWorkBean.setCollectNet(false);
            EventBus.getDefault().postSticky( netWorkBean);

        }

    }
}
